package com.dmcloud.dm.auth.api;

import com.dmcloud.common.util.reponse.ResultMessage;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@AllArgsConstructor
public class UserController {

    private HttpServletRequest request;

	// 资源API
    @RequestMapping("/api/userinfo")
    public ResponseEntity<UserInfo> getUserInfo() {
        Object principal = SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();
        User user = (User) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();
        String email = user.getUsername() + "@spring2go.com";

        UserInfo userInfo = new UserInfo();
        userInfo.setName(user.getUsername());
        userInfo.setEmail(email);

        return ResponseEntity.ok(userInfo);
    }

    @RequestMapping("/api/userinfo2")
    public ResultMessage getUserInfo2( HttpServletRequest request) {
       String header = request.getHeader("Authorization");
        String token = StringUtils.substringAfter(header, "bearer ");



        return ResultMessage.success(token);
    }
    @RequestMapping("/api/userinfo3")
    public ResultMessage getUserInfo2() {
        String header = request.getHeader("Authorization");
        String token = StringUtils.substringAfter(header, "bearer ");


        return ResultMessage.success(token);
    }


}